﻿using CM.Resources.Web.Core;
using CM.Resources.Web.Models;
using CM.UserCenter.Common;
using QL.Core;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;

namespace CM.Resources.Web.Controllers
{
    /// <summary>
    /// 文件管理
    /// </summary>
    public class FileManageController : BaseController
    {
        /// <summary>
        /// 文件列表
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public ActionResult ListFile()
        {
            //URL参数
            string Value = (string)RouteData.Values["id"];
            //文件所在的真实路径
            string FilePath = GetFilePath(Value);
            ViewBag.CurrentPath = Value;

            if (Directory.Exists(FilePath))
            {
                DirectoryInfo Dir = new DirectoryInfo(FilePath);
                DirectoryInfo[] DArrInfo = Dir.GetDirectories();
                FileInfo[] FArrInfo = Dir.GetFiles();
                ViewBag.FolderNumber = DArrInfo.Length;
                ViewBag.FileNumber = FArrInfo.Length;
                List<FileItem> FileList = new List<FileItem>();
                String RootPath = GetRealPath();    // http://*****/upload/的绝对路径

                foreach (DirectoryInfo DInfo in DArrInfo)
                {
                    FileItem Item = new FileItem();
                    String Path = DInfo.FullName;
                    Item.FileName = Path.Substring(Path.LastIndexOf("\\") + 1);
                    Item.RelativePath = Path.Replace(RootPath, "").Replace("\\", "/");
                    Item.FileSize = 0;
                    Item.CreateDate = DInfo.CreationTime;
                    Item.IsDir = true;
                    FileList.Add(Item);
                }

                foreach (FileInfo FInfo in FArrInfo)
                {
                    if ((FInfo.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden) continue;
                    FileItem Item = new FileItem();
                    String Path = FInfo.FullName;
                    Item.FileName = Path.Substring(Path.LastIndexOf("\\") + 1);
                    Item.RelativePath = Path.Replace(RootPath, "").Replace("\\", "/");
                    Item.FileSize = FInfo.Length;
                    Item.CreateDate = FInfo.CreationTime;
                    Item.IsDir = false;
                    FileList.Add(Item);
                }
                ViewBag.FileList = FileList;
            }
            else
            {
                ViewBag.ErrMsg = "请求的目录不存在";
            }
            return View();
        }
        /// <summary>
        /// 删除文件/目录
        /// </summary>
        /// <returns></returns>
        public void DelFile(string file)
        {
            if (FileHandler.ValidatePath(file) && FileHandler.ValidateRemoteIP())
            {
                //URL参数
                string Value = (string)RouteData.Values["id"];
                //文件所在的真实路径
                string FilePath = GetFilePath(Value);
                FilePath = string.Format(@"{0}/{1}", FilePath, file);

                if (System.IO.File.Exists(FilePath))
                {
                    //删除文件
                    System.IO.File.Delete(FilePath);
                }
                else if (Directory.Exists(FilePath))
                {
                    //删除目录和目录下所有文件
                    Directory.Delete(FilePath, true);
                }
                string ReturnPath = string.Format("/FileManage/ListFile/{0}", Value);
                Response.Redirect(ReturnPath);
            }
            else
            {
                Response.Redirect("/FileManage/ListFile");
            }
        }
        /// <summary>
        /// 获取虚拟路径对应的真实路径
        /// </summary>
        /// <returns></returns>
        private string GetRealPath()
        {
            return Utility.ToAbsolutePath(Configuration.Default.UploadDir);
        }
        /// <summary>
        /// 获取请求文件的真实路径
        /// </summary>
        /// <returns></returns>
        private string GetFilePath(string Value)
        {
            string RealPath = GetRealPath();
            if (Value != null && Value != "")
            {
                RealPath = string.Format(@"{0}\{1}", GetRealPath(), Value);
            }
            return RealPath;
        }
    }
}